<?php
/**
 * 小样订单
 */
quanli_require('module/base/bill/billTemp.class.php');

class pa_billTemp {

    public function execute()
    {
        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0)
        {
            switch($_REQUEST['operFlg'])
            {
                case 1: //小样订单列表
                    $this->searchTempList();
                    break;

                case 2: //保存小样价单
                    $this->saveTemp();
                    break;

                default:
                    $this->toError('参数错误');
                    break;
            }
        } else {
            $this->toError('参数错误');
        }
    }

    /**
     * 转跳到错误页
     * @param string $msg 错误信息
     */
    private function toError( $msg )
    {
        global $G_PGLOB;
        $G_PGLOB->display_error_page( $msg );
    }

    /**
     * 小样订单列表
     */
    private function searchTempList()
    {
        global $G_PGLOB;
        $G_PGLOB->smarty->display('bill/billTemp.tpl');
    }

    /**
     * 保存小样价单
     */
    private function saveTemp()
    {
        global $G_PGLOB;

        $params = array('useFlg'=>1);
        //分类
        quanli_include('module/base/goods/goodsCategory.class.php');
        $categoryHandler = new goodsCategory( $G_PGLOB->DBCA );
        $category = $categoryHandler->searchBigCategory( $params );
        $G_PGLOB->smarty->assign('category', json_encode($category));

        //物流
        quanli_require('module/base/system/sysSend.class.php');
        $sysSend = new sysSend();
        $send_array = $sysSend->searchSendType( $params );
        $G_PGLOB->smarty->assign('send_array', $send_array);

        //制作内容
        quanli_require('module/base/system/itemHandler.class.php');
        $itemHandler = new itemHandler();
        $make_result = $itemHandler->getMake();
        $make_array = array();
        if($make_result){
            foreach ($make_result as $make){
                $make_array[] = $make->itemName;
            }
        }
        $G_PGLOB->smarty->assign('make', json_encode($make_array));

        $tempInfo = null;
        $currDate = date('Y-m-d');

        if(isset($_REQUEST['code']) && !empty($_REQUEST['code'])){
            $billTemp = new billTemp( $G_PGLOB->DBCA );
            $param = array( 'tempCode' => $_REQUEST['code'] );
            $result = $billTemp->searchBillTemp($param);
            if ($result) {
                $tempInfo = $result[0];
            }
            $currDate = date('Y-m-d', strtotime($tempInfo->createTime));
            $tempInfo->dtl = $billTemp->searchTempDtl($_REQUEST['code']);

            $temp_annex = null;
            $resAnnex = $billTemp->searchAnnex($_REQUEST['code']);
            if($resAnnex){
                foreach ($resAnnex as $annex){
                    $annex->path = G_IMAGE_URL.'goods/wx/'.$annex->fileName;
                    $temp_annex[$annex->categoryName][] = $annex;
                }
            }
            $tempInfo->annex = $temp_annex;
        }

        $G_PGLOB->smarty->assign('tempInfo', $tempInfo);
        $G_PGLOB->smarty->assign('currDate', $currDate);
        $G_PGLOB->smarty->display('bill/billTempAdd.tpl');
    }

}